
#
# A Production Rule System implemented in Python.
#
# (C) Copyright 2006-2008, Johan Lindberg
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA
#

import rete

from code import *
from codeop import CommandCompiler

ruleengine = None

class Console(InteractiveConsole):
    def __init__(self):
        InteractiveInterpreter.__init__(self)
        self.locals = { "__name__" : "pyRete.console",
                        "__doc__": "pyRete is a small and simple forward chaining Rule Engine implemented in Python.",
                        
                        "pyRete" : pyRete,
                        "Rule": self.Rule,
                        
                        "assert_fact" : ruleengine.assert_fact,
                        "retract_fact" : ruleengine.retract_fact,
                        "modify_fact" : ruleengine.modify_fact,
                        
                        "list_facts" : ruleengine.list_facts,
                        "list_rules" : ruleengine.list_rules,
                        "list_watch_items" : ruleengine.list_watch_items,
                        
                        "set_break" : ruleengine.set_break,
                        "remove_break" : ruleengine.remove_break,
                        "show_breaks" : ruleengine.show_breaks,
                        
                        "run" : ruleengine.run,
                        "clear" : ruleengine.clear,
                        "reset" : ruleengine.reset,
                        
                        "set_conflict_resolution_strategy" : ruleengine.set_conflict_resolution_strategy,
                        "show_agenda" : ruleengine.show_agenda,
                        "clear_focus_stack" : ruleengine.clear_focus_stack,
                        "get_focus" : ruleengine.get_focus,
                        "get_focus_stack" : ruleengine.get_focus_stack,
                        "set_focus" : ruleengine.set_focus,
                        "pop_focus" : ruleengine.pop_focus,
                        
                        "load" : execfile,
                        
                        "watch" : ruleengine.watch,
                        "unwatch" : ruleengine.unwatch, }
                        
        # add flags to locals
        for flag in [flag for flag in dir(pyRete.flags) if not flag.startswith("_")]:
            self.locals[flag] = flag
            
        self.filename = "<pyRete.console>"
        self.resetbuffer()
        
    def interact(self, banner=None):
        ps1 = "pyRete> "
        ps2 = "....... "
        
        more = 0
        while 1:
            try:
                if more:
                    prompt = ps2
                    
                else:
                    prompt = ps1
                    
                try:
                    line = self.raw_input(prompt)
                    
                except EOFError:
                    self.write("\n")
                    break
                    
                else:
                    more = self.push(line)
                    
            except KeyboardInterrupt:
                self.write("\nKeyboardInterrupt\n")
                self.resetbuffer()
                more = 0
                
    def Rule(self, function):
        function._is_rule = True
        function._source = "\n".join(self.buffer)
        
        ruleengine.add_rule(function)
        
        return function
        
def interact(instance):
    global ruleengine
    ruleengine = instance
    
    try:
        import readline
        
    except ImportError:
        pass
        
    Console().interact()
    
if __name__ == '__main__':
    interact(pyRete.RuleEngine())
    